package thread;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class demo12 {

        public static int[] moveZeroes(int[] nums) {
            int[] result = Arrays.copyOf(nums, nums.length);
            AtomicInteger writeIndex = new AtomicInteger();
            // 使用IntStream来遍历数组元素
            IntStream.of(result).filter(num -> num!= 0).forEach(num -> result[writeIndex.getAndIncrement()] = num);
            while (writeIndex.get() < result.length) {
                result[writeIndex.getAndIncrement()] = 0;
            }
            return result;
        }

        public static void main(String[] args) {
            int[] nums = {0, 1, 0, 3, 12};
            int[] result = moveZeroes(nums);
            System.out.println(Arrays.toString(result));
        }
}
